%
% Get the nearest neighbor path with given starting index
%
function [p l] = getNearestPath(sites, idx, distances)

[len n] = size(sites);

visited = zeros(len, 1);

currIdx = idx;

l = 0;
p = [];

theMax = max(max(distances));

for i = 1:len
    visited(currIdx) = 1;
    p = [p; sites(currIdx, :)];
    
    min = theMax;
    
    for j = 1:len
        if j ~= currIdx && visited(j) == 0
            if distances(currIdx, j) < min 
                min = distances(currIdx, j);
                nextIdx = j;
            end
        end
    end
    
    if i ~= len % the last point is max
		l = l + min;
    end
    
    currIdx = nextIdx;
end